package com.amigo.online.provider.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.amigo.online.provider.manager.mapper.AdvRecommendApplyAreaTblMapper;
import com.amigo.online.provider.manager.mapper.AdvRecommendApplyTblMapper;
import com.amigo.online.provider.manager.mapper.AdvRecommendSourceTblMapper;
import com.amigo.online.provider.manager.mapper.AdvRecommendStockTblMapper;
import com.amigo.online.provider.manager.mapper.AdvRecommendVideoTblMapper;
import com.amigo.online.provider.manager.mapper.VideoMapper;
import com.amigo.online.provider.manager.model.entity.AdvRecommendApplyAreaTbl;
import com.amigo.online.provider.manager.model.entity.AdvRecommendApplyTbl;
import com.amigo.online.provider.manager.model.entity.AdvRecommendSourceTbl;
import com.amigo.online.provider.manager.model.entity.AdvRecommendStockTbl;
import com.amigo.online.provider.manager.model.entity.AdvRecommendVideoTbl;
import com.amigo.online.provider.manager.model.entity.Video;
import com.amigo.online.provider.manager.result.AdvRecommendApplyInfo;
import com.amigo.online.provider.manager.service.AdvRecommendApplyTblService;
import com.amigo.online.provider.manager.util.OrderUtils;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdvRecommendApplyTblServiceImpl implements AdvRecommendApplyTblService {

    @Resource
    private AdvRecommendApplyAreaTblMapper advRecommendApplyAreaTblMapper;// 地区中间表

    @Resource
    private AdvRecommendApplyTblMapper advRecommendApplyTblMapper;// 申请表

    @Resource
    private AdvRecommendSourceTblMapper advRecommendSourceTblMapper;// 资源表

    @Resource
    private AdvRecommendStockTblMapper advRecommendStockTblMapper;// 库存表

    @Resource
    private AdvRecommendVideoTblMapper advRecommendVideoTblMapper;// video资源表

    @Resource
    private VideoMapper videoMapper;

    @Override
    public List<AdvRecommendSourceTbl> findRecommendSourceList(Long advId) {
        return advRecommendSourceTblMapper.findAdvHomeSourceList(advId);
    }

    @Override
    public boolean saveAdvRecommendSourceTbl(AdvRecommendSourceTbl advRecommendSourceTbl) {
        if (advRecommendSourceTblMapper.saveAdvRecommendSourceTbl(advRecommendSourceTbl) <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean updateAdvRecommendSourceTbl(AdvRecommendSourceTbl advRecommendSourceTbl) {
        if (advRecommendSourceTblMapper.updateAdvRecommendSourceTbl(advRecommendSourceTbl) <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean deleteAdvRecommendSourceTbl(AdvRecommendSourceTbl advRecommendSourceTbl) {
        if (advRecommendSourceTblMapper.deleteAdvRecommendSourceTbl(advRecommendSourceTbl) <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean savaAdvRecommendStockTbl(AdvRecommendStockTbl ahst) {
        if (advRecommendStockTblMapper.savaAdvRecommendStockTbl(ahst) <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean updateAdvRecommendStockTbl(AdvRecommendStockTbl ahst) {
        if (advRecommendStockTblMapper.updateAdvRecommendStockTbl(ahst) <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean deleteAdvRecommendStockTbl(Long id) {
        if (advRecommendStockTblMapper.deleteAdvRecommendStockTbl(id) <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<AdvRecommendApplyTbl> findAdvRecommendApplyTbl(AdvRecommendApplyTbl ahat) {

        return advRecommendApplyTblMapper.findAdvRecommendApplyTbl(ahat);
    }

    @Override
    public AdvRecommendApplyTbl saveAdvRecommendApplyTbl(AdvRecommendApplyTbl ahat) {
        String orderId = OrderUtils.createOrderNumber();
        ahat.setOrderId(orderId);
        ahat.setStatus(0);
        ahat.setCreatedTime(System.currentTimeMillis() / 1000);
        if (advRecommendApplyTblMapper.saveAdvRecommendApplyTbl(ahat) <= 0) {
            return null;
        } else {
            List<Long> areaId = ahat.getAreaIds();
            Long advId = advRecommendApplyTblMapper.findAdvRecommendApplyById(ahat.getOrderId());
            if (areaId != null && areaId.size() > 0) {
                List<AdvRecommendApplyAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvRecommendApplyAreaTbl e = new AdvRecommendApplyAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    e.setCreatedTime(System.currentTimeMillis() / 1000);
                    list.add(e);
                }
                if (advRecommendApplyAreaTblMapper.insertList(list) > 0) {
                    return advRecommendApplyTblMapper.selectByOrderId(orderId);
                }
            } else {
                return null;
            }
        }
        return null;

    }

    @Override
    public boolean updateAdvRecommendApplyTbl(AdvRecommendApplyTbl ahat) {
        System.out.println(ahat.getId());
        ahat.setUpdatedTime(System.currentTimeMillis() / 1000);
        AdvRecommendApplyTbl selectById = advRecommendApplyTblMapper.selectById(ahat.getId());
        if (selectById.getStatus() == null || selectById.getStatus().equals(1)) {
            return false;
        }
        if (advRecommendApplyTblMapper.updateAdvRecommendApplyTbl(ahat) > 0) {
            List<Long> areaId = ahat.getAreaIds();// 获取地址id
            if (areaId != null && areaId.size() > 0) {
                Long advId = ahat.getId();// 申请表id
                AdvRecommendApplyAreaTbl ex = new AdvRecommendApplyAreaTbl();
                ex.setAdvId(advId);// 申请表id
                ex.setDeleted(1);
                ex.setDeletedTime(System.currentTimeMillis() / 1000);
                advRecommendApplyAreaTblMapper.deleteAdvRecommendApplyAreaTbl(ex);// 删除地址表的信息
                List<AdvRecommendApplyAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvRecommendApplyAreaTbl e = new AdvRecommendApplyAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    list.add(e);
                }
                if (advRecommendApplyAreaTblMapper.insertList(list) > 0) {
                    return true;
                }
            }
        } else {
            return false;
        }
        return false;

    }

    @Override
    public boolean deleteAdvRecommendApplyTbl(AdvRecommendApplyTbl ahat) {
        ahat.setDeletedTime(System.currentTimeMillis() / 1000);
        ahat.setDeleted(1);
        int i = advRecommendApplyTblMapper.deleteAdvRecommendApplyTbl(ahat);
        if (i <= 0) {
            return false;
        } else {

            return true;
        }
    }

    @Override
    public boolean checkAdv(AdvRecommendApplyTbl ahat) {

        Long id = ahat.getId();
        advRecommendApplyTblMapper.updateAdvRecommendApplyTbl(ahat);
        if (ahat.getStatus() == 1) {
            AdvRecommendApplyTbl advRecommendApplyTbl = advRecommendApplyTblMapper.selectById(id);
            AdvRecommendStockTbl as = new AdvRecommendStockTbl();
            as.setAdvId(advRecommendApplyTbl.getId());
            as.setApplyNumber(advRecommendApplyTbl.getTotalNumber());
            as.setRemainingNumber(advRecommendApplyTbl.getTotalNumber());
            as.setUsedNumber(0);
            as.setUsedNumber(0);
            if (advRecommendStockTblMapper.insert(as) > 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;

    }

    @Override
    public AdvRecommendApplyInfo findAdvRecommendApplyTblParticulars(Long id) {
        //List<AdvRecommendSourceTbl> advRecommendSourceTbl = advRecommendSourceTblMapper.findAdvHomeSourceList(id);
        AdvRecommendVideoTbl advRecommendVideoTbl = advRecommendVideoTblMapper.selectAdvRecommendVideo(id);
        AdvRecommendStockTbl advRecommendStockTbl = advRecommendStockTblMapper.findAdvRecommendStockTbl(id);
        List<AdvRecommendApplyAreaTbl> advRecommendApplyAreaTblList = advRecommendApplyAreaTblMapper
                .findAdvRecommendApplyAreaTblList(id);
        AdvRecommendApplyInfo ahi = new AdvRecommendApplyInfo();
        ahi.setAdvRecommendApplyAreaTblList(advRecommendApplyAreaTblList);
        ahi.setAdvRecommendVideoTbl(advRecommendVideoTbl);
        ahi.setAdvRecommendStockTbl(advRecommendStockTbl);
        return ahi;
    }

    @Override
    public boolean saveAdvRecommendVideoTbl(AdvRecommendVideoTbl advRecommendVideoTbl) {

        Video vd = new Video();
        vd.setVideo(advRecommendVideoTbl.getVideoUrl());
        vd.setCover(advRecommendVideoTbl.getVideoImgUrl());
        vd.setType(1);
        vd.setAds(1);
        vd.setTitle("...");
        vd.setWidth(advRecommendVideoTbl.getVideoWidth());
        vd.setHeight(advRecommendVideoTbl.getVideoHeight());
        vd.setCreatedTime(System.currentTimeMillis() / 1000);
        if (videoMapper.saveVideo(vd) > 0) {
            System.out.println(vd.getId());
            advRecommendVideoTbl.setVideoId(vd.getId());
            advRecommendVideoTbl.setCreatedTime(System.currentTimeMillis() / 1000);
            if (advRecommendVideoTblMapper.insert(advRecommendVideoTbl) <= 0) {
                return false;
            } else {

                return true;
            }
        } else {
            return false;
        }
    }


    @Override
    public boolean updateAdvRecommendVideoTbl(AdvRecommendVideoTbl advRecommendVideoTbl) {

        if (advRecommendVideoTbl.getVideoUrl() != null || !advRecommendVideoTbl.getVideoUrl().isEmpty()) {
            Video vd = new Video();
            vd.setId(advRecommendVideoTbl.getVideoId());
            vd.setVideo(advRecommendVideoTbl.getVideoUrl());
            vd.setCover(advRecommendVideoTbl.getVideoImgUrl());
            vd.setWidth(advRecommendVideoTbl.getVideoWidth());
            vd.setHeight(advRecommendVideoTbl.getVideoHeight());
            vd.setUpdatedTime(System.currentTimeMillis() / 1000);
            videoMapper.updateVideo(vd);
        }
        return advRecommendVideoTblMapper.updateAdvRecommendVideoById(advRecommendVideoTbl) > 0;
    }

    @Override
    public boolean deleteAdvRecommendVideoTbl(AdvRecommendVideoTbl advRecommendVideoTbl) {

        Video vd = new Video();
        System.out.println(advRecommendVideoTbl.getVideoId());
        vd.setId(advRecommendVideoTbl.getVideoId());
        vd.setDeleted(1);
        vd.setDeletedTime(System.currentTimeMillis() / 1000);
        advRecommendVideoTbl.setDeleted(1);
        advRecommendVideoTbl.setDeletedTime(System.currentTimeMillis() / 1000);
        if (videoMapper.updateVideo(vd) > 0) {
            return advRecommendVideoTblMapper.updateAdvRecommendVideoById(advRecommendVideoTbl) > 0;
        } else {
            return false;
        }

    }

}
